package com.jin.mybatis_plus;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;

//代码自动生成器
public class generator {
    public static void main(String[] args) {
        //需要构建一个代码自动生成器对象
        AutoGenerator autoGenerator = new AutoGenerator();
        //配置策略
        //1、全局配置
        GlobalConfig globalConfig = new GlobalConfig();
        String Path = System.getProperty("user.dir");
        globalConfig.setAuthor("jinzhuo");
        globalConfig.setOutputDir(Path+"/src/main/java");
        //是否打开资源管理器
        globalConfig.setOpen(false);
        //是否覆盖原来代码
        globalConfig.setFileOverride(false);
        //去serviceI前缀
        globalConfig.setServiceName("%sService");
        globalConfig.setDateType(DateType.ONLY_DATE);
        globalConfig.setIdType(IdType.ASSIGN_ID);

        autoGenerator.setGlobalConfig(globalConfig);

        //2、设置数据源
        DataSourceConfig sourceConfig = new DataSourceConfig();
        sourceConfig.setDbType(DbType.MYSQL);
        sourceConfig.setUrl("jdbc:mysql://localhost:3306/mybatis_plus?useSSL=false&usrUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        sourceConfig.setDriverName("com.mysql.cj.jdbc.Driver");
        sourceConfig.setUsername("root");
        sourceConfig.setPassword("123456");
        autoGenerator.setDataSource(sourceConfig);

        //3、包的配置
        PackageConfig packageConfig = new PackageConfig();
        packageConfig.setModuleName("batis_plus");
        packageConfig.setEntity("entity");
        packageConfig.setMapper("mapper");
        packageConfig.setService("service");
        packageConfig.setController("controller");
        packageConfig.setServiceImpl("serviceImpl");
        packageConfig.setParent("jj");
        autoGenerator.setPackageInfo(packageConfig);

        //4、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(true);

        strategy.setInclude("user");//设置要映射的表名
        strategy.setLogicDeleteFieldName("delete_flag");
        //自动填充
        TableFill create_date = new TableFill("create_date", FieldFill.INSERT);
        TableFill update_date = new TableFill("update_date", FieldFill.INSERT_UPDATE);
        ArrayList<TableFill> list = new ArrayList<>();
        list.add(create_date);
        list.add(update_date);
        strategy.setTableFillList(list);

        autoGenerator.execute();//执行


    }
}
